﻿Public Class frTimKiemThuChi
    Dim sotien1, sotien2, loai, tk, ngthu, nglap, thang, nam As Integer
    Dim ngay As Date

    Private Sub btntrovedspt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntrovedspt.Click
        Me.Close()
    End Sub
    

    Private Sub KiemTraThamSo()
        sotien1 = -1
        sotien2 = -1
        loai = -1
        tk = -1
        ngthu = -1
        nglap = -1
        thang = -1
        nam = -1
        ngay = Date.MinValue

        If ckbTatCa.Checked = False Then
            If CkbLoai.Checked = True Then
                loai = cbxloaidspt.SelectedValue
            End If
            If CkbNguoiLap.Checked = True Then
                nglap = cbxnguoitao.SelectedValue
            End If
            If CkbNguoiThu.Checked = True Then
                ngthu = cbxnguoithu.SelectedValue
            End If
            If ckbSoTien.Checked = True Then
                Dim t As Integer
                If Integer.TryParse(txtSoTienTu.Text, t) = False Then
                    MessageBox.Show("Kiểm tra số tiền!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Exit Sub
                End If
                sotien1 = t
                If Integer.TryParse(txtSoTienDen.Text, t) = False Then
                    MessageBox.Show("Kiểm tra số tiền!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Exit Sub
                End If
                sotien2 = t
            End If
            If CkbTaiKhoan.Checked = True Then
                tk = cbxtaikhoandspt.SelectedValue
            End If
            If CkbThoigian.Checked = True Then
                If rbtnthang.Checked = True Then
                    thang = CbxThang.SelectedItem
                    nam = Integer.Parse(cbxNam.SelectedItem)
                Else
                    ngay = dtpngaydspt.Value.Date
                End If
            End If
        End If
    End Sub


    Private Sub frTimKiemThuChi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rdbThu.Checked = True
        nam = Date.Now.Year
        Dim i = 2
        While i >= 0
            cbxNam.Items.Add(nam - i)
            i = i - 1
        End While
        cbxNam.SelectedIndex = 2
        CbxThang.SelectedIndex = Now.Month - 1

        cbxnguoitao.DataSource = XL_NguoiDung.LayDS()
        cbxnguoitao.ValueMember = "ID"
        cbxnguoitao.DisplayMember = "Username"

        cbxnguoithu.DataSource = XL_NguoiDung.LayDS()
        cbxnguoithu.ValueMember = "ID"
        cbxnguoithu.DisplayMember = "Username"

        cbxtaikhoandspt.DataSource = XL_TaiKhoan.LayDS(Me.Tag)
        cbxtaikhoandspt.ValueMember = "ID"
        cbxtaikhoandspt.DisplayMember = "TenTK"

        ckbTatCa.Checked = True


    End Sub

    Private Sub btntim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntim.Click
        If rdbThu.Checked = True Then
            KiemTraThamSo()
            dgvdspthu.DataSource = XL_PhieuThu.LayDS2(sotien1, sotien2, loai, tk, ngthu, nglap, thang, nam, ngay)
        ElseIf rdbChi.Checked = True Then
            KiemTraThamSo()
            dgvdspthu.DataSource = XL_PhieuChi.LayDS2(sotien1, sotien2, loai, tk, ngthu, nglap, thang, nam, ngay)
        End If
        For i = 0 To dgvdspthu.ColumnCount - 1
            dgvdspthu.Columns(i).SortMode = DataGridViewColumnSortMode.Automatic
        Next
    End Sub

    Private Sub rdbThu_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbThu.CheckedChanged
        If rdbThu.Checked = True Then
            cbxloaidspt.DataSource = XL_LoaiThu.LayDS()
        Else
            cbxloaidspt.DataSource = XL_LoaiChi.LayDS()
        End If
        cbxloaidspt.ValueMember = "ID"
        cbxloaidspt.DisplayMember = "Loai"
    End Sub

    Private Sub btnbc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbc.Click
        Dim f As New F_Report
        f.Tag = Me.Tag
        f.ShowDialog()
    End Sub

    Private Sub btnvebd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnvebd.Click
        Dim f As New F_Report
        f.Tag = Me.Tag
        f.ShowDialog()
    End Sub
End Class